<?php
namespace App\Services\V1;

use App\Http\Controllers\API\FormatController;
use App\Models\Shops;
use App\Services\BaseService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

#负责处理相关的业务逻辑
class AdminShopsServer extends BaseService{


        private object $User;
        private Request $Request;
        private array $Header;
        private string $Lang;
        private int $Limit;
        private int $Page;

        public function __construct(FormatController $forMat)
        {
           $UserData = $forMat->getAdmin();
            if($UserData['status'] === false){
                Message(__('fail.user_not_power'));
            }
           $this->User = $UserData['user'] ?? (object)[];
           $this->Request = $forMat->getRequest();
           $this->Header = $forMat->getHeader();
           $this->Lang = $forMat->getLang();
           $this->Limit = $forMat->getLimit();
           $this->Page = $forMat->getPage();
           if((int)$this->User->power !== 1){//只有超级管理才能进行一下操作
               Message(__('fail.user_not_power').'X');
           }
        }

        private function Model(): Builder
        {
            return Shops::query();
        }


        public function list():object{//列表
            $request = $this->Request;
            $query = $this->Model();
            if(!empty($request->id)){
                return $this->details();
            }
            try{
                $input = $request->validate([
                    'keyword'=>'required|string|max:255'
                ]);
                $keyword = $input['keyword'];
                $query->where('name','like',"%$keyword%");
            }catch (\Throwable $e){}
            return $query->get();
        }

        public function add():bool{//添加
            $input = $this->Request->validate([
                'admin_id'=>'required|integer|exists:z_admin_user,id',//店铺所属的管理员ID
                'name'=>"required|string",//店铺名称
                'description'=>"string",//店铺描述
                'logo'=>"required|string",//店铺Logo的图片路径或URL
                'address'=>"required|string",//店铺详细地址
                'phone'=>"required|string|max:11",//联系电话
                'opening_hours'=>"required|string",//营业时间
            ],[
                'name'=>__('validate.shops.name').__("validate.error"),
                'description'=>__('validate.shops.description').__("validate.error"),
                'logo'=>__('validate.shops.logo').__("validate.error"),
                'address'=>__('validate.shops.address').__("validate.error"),
                'phone'=>__('validate.shops.phone').__("validate.error"),
                'opening_hours'=>__('validate.shops.opening_hours').__("validate.error"),
            ]);
            if($this->Model()->where('admin_id',$input['admin_id'])->exists()){
                Message(__('fail.admin_has_shops'));
            }

            $this->setFileActive($input['logo']);
            $add = $this->Model()->create($input);
            if(!$add) Message(__('fail.create_shops'));
            $this->Model()->where('admin_id',$input['admin_id'])->update(['shops_id'=>$add->id]);
            return true;
        }

        public function edit():bool{//编辑
            $input = $this->Request->validate([
                'id'=>'required|integer',
                'admin_id'=>'required|integer|exists:z_admin_user,id',//店铺所属的管理员ID
                'name'=>"required|string",//店铺名称
                'description'=>"string",//店铺描述
                'logo'=>"required|string",//店铺Logo的图片路径或URL
                'address'=>"required|string",//店铺详细地址
                'phone'=>"required|string|max:11",//联系电话
                'opening_hours'=>"required|string",//营业时间
            ],[
                'name'=>__('validate.shops.name').__("validate.error"),
                'description'=>__('validate.shops.description').__("validate.error"),
                'logo'=>__('validate.shops.logo').__("validate.error"),
                'address'=>__('validate.shops.address').__("validate.error"),
                'phone'=>__('validate.shops.phone').__("validate.error"),
                'opening_hours'=>__('validate.shops.opening_hours').__("validate.error"),
            ]);

            $id = $input['id'];
            unset($input['id']);
            $oldLogo = $this->Model()->find($id)->logo;

            $this->setFileActive($input['logo'],$oldLogo);
            $edit = $this->Model()->where('id',$id)->update($input);
            if(!$edit) Message(__('fail.edit_shops'));
            return true;
        }

        public function del():bool{//删除
            $input = $this->Request->validate([
                'id'=>'required|integer'
                ]);
            $id = $input['id'];
            $oldShops = $this->Model()->find($id);
            if(!$oldShops) Message(__('fail.data_not_exist'));
            $this->delFileActive($oldShops->logo);
            $del = $oldShops->delete();
            if(!$del) Message(__('fail.del_shops'));
            return true;
        }

        public function details():object{//详情
            $input = $this->Request->validate([
                'id'=>'required|integer'
            ]);
            $id = $input['id'];
            $data = $this->Model()->find($id);
            if(!$data) Message(__('fail.data_not_exist'));
            return $data;
        }
}
